﻿#region Copyright 2011 Mike Goatly
// This source is subject to the the MIT License (MIT)
// All rights reserved.
#endregion

namespace Lifti.Tests.Persistence.DataFileManagerTests
{
    using System.IO;

    using Lifti.Persistence;
    using Lifti.Persistence.IO;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Tests for the construction of the IO manager.
    /// </summary>
    [TestClass]
    public class Construction
    {
        /// <summary>
        /// The path to the test file.
        /// </summary>
        private const string Path = "testindex.dat";

        /// <summary>
        /// Cleans up before and after each test.
        /// </summary>
        [TestCleanup]
        [TestInitialize]
        public void TestCleanup()
        {
            if (File.Exists(Path))
            {
                File.Delete(Path);
            }
        }

        /// <summary>
        /// If the file doesn't exist when the IOManager is created, the IsNewFile property should
        /// return true.
        /// </summary>
        [TestMethod]
        public void ShouldReportNewFileCreation()
        {
            using (var manager = new DataFileManager(Path))
            {
                Assert.IsTrue(manager.IsNewFile);
            }

            Assert.IsTrue(File.Exists(Path));
        }

        /// <summary>
        /// If the file exists when the IOManager is created, the IsNewFile property should
        /// return false.
        /// </summary>
        [TestMethod]
        public void ShouldReportThatFileAlreadyExists()
        {
            using (var writer = File.CreateText(Path))
            {
                writer.Write("Test");
            }

            using (var manager = new DataFileManager(Path))
            {
                Assert.IsFalse(manager.IsNewFile);
            }

            Assert.IsTrue(File.Exists(Path));
        }
    }
}
